package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.domain.MessageStation;
import cn.itsource.ymcc.dto.StationDto;
import cn.itsource.ymcc.service.IMessageStationService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(topic = "message",selectorExpression = "station",consumerGroup = "service-course-producer",messageModel = MessageModel.CLUSTERING)
public class StationConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMessageStationService iMessageStationService;
    @Override
    public void onMessage(MessageExt messageExt) {
        String msg = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        StationDto dto = JSON.parseObject(msg, StationDto.class);
        MessageStation messageStation = new MessageStation();
        dto.getUserIds().forEach(userId->{
            messageStation.setUserId(userId);
            BeanUtils.copyProperties(dto,messageStation);
            iMessageStationService.insert(messageStation);
        });
    }
}
